<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_taobaoke/model')->方法名();
*/
namespace jy_jdk;
defined('BY_JYA') or exit('error');
class model extends \plugin{	
	// 京东联盟生成签名
	/**
		method 接口名称
	**/
	public function sign_method($method,$data=array()){
		date_default_timezone_set("PRC");
		$basic=$this->plugin_setting('basic');
		if(!$basic['appkey'] || !$basic['secretkey']){
			exi('请先到基本设置里面添加应用的appkey和secretkey','error');
		}
		$sign_method = array(
						'app_key'=> $basic['appkey'],
						'timestamp'=> date('Y-m-d H:i:s'),
						'format' => 'json',
						'method' => $method,
						'v' => '1.0',
						'sign_method'=>'md5',
					);
		$sign_method = array_merge($sign_method,$data);
		ksort($sign_method);
		$data = $basic['secretkey'];
		foreach ($sign_method as $key => $value) {
			$data .= trim($key) .trim($value);
		}
		$data .= $basic['secretkey'];
		$sign_method['sign'] = strtoupper(md5($data));
		return $sign_method;
	}
	//商品详情查询接口【申请】
	public function goodsInfobigfield($sku){
		$param = array();
		$data['goodsReq'] = array('skuIds'=>$sku,'fields'=>'');
		$param['360buy_param_json'] = json_encode($data);
		$GoodsList = $this->sign_method('jd.union.open.goods.bigfield.query',$param);	
		$res = $this->curl_post('https://api.jd.com/routerjson', $GoodsList);
		$res = json_decode($res,true);
		if($res['code']!=0){
			return '未获得《商品详情查询接口【申请】》API权限，请移步京东联盟申请';
		}
		$content=$res['jd_union_open_goods_bigfield_query_responce']['queryResult'];
		$content = json_decode($content,true);
		if($content['code']!=200){
			//exi('获取详情失败，错误代码：'.$content['code'],'error');
		}
		return $content['data'];
	}
	public function goodsInfo($sku){
		$param = array();
		$data['skuIds'] = $sku;
		$param['360buy_param_json'] = json_encode($data);
		$GoodsList = $this->sign_method('jd.union.open.goods.promotiongoodsinfo.query',$param);
		$res = $this->curl_post('https://api.jd.com/routerjson', $GoodsList);
		$res = json_decode($res,true);
		if($res['error_response'] && $res['error_response']['code']!=200){
			exi('采集失败，错误代码：'.$res['error_response']['code'].','.$res['error_response']['zh_desc'],'error');
		}
		$content=$res['jd_union_open_goods_promotiongoodsinfo_query_responce']['queryResult'];
		$content = json_decode($content,true);
		if($content['code']!=200 || !$content['data']){
			exi('采集失败，错误代码：'.$content['code'].','.$content['message'],'error');
		}
		return $content['data'];
	}
	public function getgoods_cats(){
		$param = array();
		$data['parentId'] = 0;
		$data['grade'] = 3;
		$param['req'] = json_encode($data);
		$GoodsList = $this->sign_method('jd.union.open.category.goods.get',$param);
		$res = $this->curl_post('https://api.jd.com/routerjson', $GoodsList);
		$res = json_decode($res,true);
		if($res['error_responce']){
			return false;
			exi('获取分类失败：'.$res['error_responce']['code'].','.$res['error_responce']['zh_desc'],'error');
		}
		$content=$res['jd_union_open_category_goods_get_responce']['getResult'];
		$content = json_decode($content,true);
		if($content['code']!=200){
			return false;
			exi('获取分类失败：'.$content['code'].'~~'.$content['message'],'error');
		}
		return $content['data'];
	}
	public function goodslist($post=array()){
		$param = array();
		$data=array();
		$data['cid1'] = $post['cid1']?:'';
		$data['cid2'] = $post['cid12']?:'';
		$data['cid3'] = $post['cid13']?:'';
		$data['pageIndex'] = $post['pageIndex']?:1;
		$data['keyword'] = $post['keyword']?:'';
		$data['pricefrom'] = $post['pricefrom']?:'';
		$data['priceto'] = $post['priceto']?:'';
		$data['commissionShareStart'] = $post['commissionShareStart']?:'';
		$data['commissionShareEnd'] = $post['commissionShareEnd']?:'';
		$data['owner'] = $post['owner']?:'g';
		$data['sortName'] = $post['sortName']?:'price';
		$data['sort'] = $post['sort']?:'desc';
		$data['isCoupon'] = $post['isCoupon']?:'1';
		$data['isPG'] = $post['isPG']?:'1';
		$data['pingouPriceStart'] = $post['pingouPriceStart'];
		$data['pingouPriceEnd'] = $post['pingouPriceEnd'];
		$data['isHot'] = $post['isHot']?:'1';
		$data['brandCode'] = $post['brandCode'];
		$data['shopId'] = $post['shopId'];
		$data['shopLevelFrom'] = $post['shopLevelFrom']?:'3.5';
		$param['360buy_param_json'] = json_encode($data);
		$GoodsList = $this->sign_method('jd.union.open.goods.query',$param);
		$res = $this->curl_post('https://api.jd.com/routerjson', $GoodsList);
		$res = json_decode($res,true);
		if($res['error_responce']){
			exi('获取商品失败：'.$res['error_responce']['code'].'~~'.$res['error_responce']['zh_desc'],'error');
		}
		if($res['jd_union_open_goods_query_responce']['code']!=200){
			$errorresult=json_decode($res['jd_union_open_goods_query_responce']['queryResult'],true);
			exi('获取商品失败：'.$errorresult['code'].'~~'.$errorresult['message'],'error');
		}
		$content=$res['jd_union_open_goods_query_responce']['queryResult'];
		$content = json_decode($content,true);
		return $content['data'];
	}
	public function getgoodslist_jin($post=array()){
		$param = array();
		$data=array();
		$data['cid1'] = $post['cid1']?:'';
		$data['cid2'] = $post['cid12']?:'';
		$data['cid3'] = $post['cid13']?:'';
		$data['pageIndex'] = $post['pageIndex']?:1;
		$data['eliteId'] = $post['eliteId']?:'';
		$data['sortName'] = $post['sortName']?:'price';
		$data['sort'] = $post['sort']?:'desc';
		$param['goodsReq'] = json_encode($data);
		$GoodsList = $this->sign_method('jd.union.open.goods.jingfen.query',$param);
		$res = $this->curl_post('https://api.jd.com/routerjson', $GoodsList);
		$res = json_decode($res,true);
		if($res['error_responce']){
			exi('获取商品失败：'.$res['error_responce']['code'].'~~'.$res['error_responce']['zh_desc'],'error');
		}
		if($res['jd_union_open_goods_query_responce']['code']!=200){
			$errorresult=json_decode($res['jd_union_open_goods_query_responce']['queryResult'],true);
			exi('获取商品失败：'.$errorresult['code'].'~~'.$errorresult['message'],'error');
		}
		$content=$res['jd_union_open_goods_jingfen_query_responce']['queryResult'];
		$content = json_decode($content,true);
		return $content['data'];
	}
	public function getgoodslist_miao($post=array()){
		$param = array();
		$data=array();
		$data['cid1'] = $post['cid1']?:'';
		$data['cid2'] = $post['cid12']?:'';
		$data['cid3'] = $post['cid13']?:'';
		$data['pageIndex'] = $post['pageIndex']?:1;
		$data['isBeginSecKill'] = $post['isBeginSecKill']?:'';
		$data['secKillPriceFrom'] = $post['secKillPriceFrom']?:'';
		$data['secKillPriceTo'] = $post['secKillPriceTo']?:'';
		$data['owner'] = $post['owner']?:'';
		$data['commissionShareFrom'] = $post['commissionShareFrom']?:'';
		$data['commissionShareTo'] = $post['commissionShareTo']?:'';
		$data['sortName'] = $post['sortName']?:'seckillPrice';
		$data['sort'] = $post['sort']?:'desc';
		$param['goodsReq'] = json_encode($data);
		$GoodsList = $this->sign_method('jd.union.open.goods.seckill.query',$param);
		$res = $this->curl_post('https://api.jd.com/routerjson', $GoodsList);
		$res = json_decode($res,true);
		if($res['error_responce']){
			exi('获取商品失败：'.$res['error_responce']['code'].'~~'.$res['error_responce']['zh_desc'],'error');
		}
		if($res['jd_union_open_goods_query_responce']['code']!=200){
			$errorresult=json_decode($res['jd_union_open_goods_query_responce']['queryResult'],true);
			exi('获取商品失败：'.$errorresult['code'].'~~'.$errorresult['message'],'error');
		}
		$content=$res['jd_union_open_goods_seckill_query_responce']['queryResult'];
		$content = json_decode($content,true);
		return $content['data'];
	}
	public function getgoodslist_xue($post=array()){
		$param = array();
		$data=array();
		$data['cid1'] = $post['cid1']?:'';
		$data['cid2'] = $post['cid12']?:'';
		$data['cid3'] = $post['cid13']?:'';
		$data['pageIndex'] = $post['pageIndex']?:1;
		$data['stuPriceFrom'] = $post['stuPriceFrom']?:'';
		$data['stuPriceTo'] = $post['stuPriceTo']?:'';
		$data['owner'] = $post['owner']?:'';
		$data['commissionShareFrom'] = $post['commissionShareFrom']?:'';
		$data['commissionShareTo'] = $post['commissionShareTo']?:'';
		$data['sortName'] = $post['sortName']?:'stuPrice';
		$data['sort'] = $post['sort']?:'desc';
		$param['goodsReq'] = json_encode($data);
		$GoodsList = $this->sign_method('jd.union.open.goods.stuprice.query',$param);
		$res = $this->curl_post('https://api.jd.com/routerjson', $GoodsList);
		$res = json_decode($res,true);
		if($res['error_responce']){
			exi('获取商品失败：'.$res['error_responce']['code'].'~~'.$res['error_responce']['zh_desc'],'error');
		}
		if($res['jd_union_open_goods_query_responce']['code']!=200){
			$errorresult=json_decode($res['jd_union_open_goods_query_responce']['queryResult'],true);
			exi('获取商品失败：'.$errorresult['code'].'~~'.$errorresult['message'],'error');
		}
		$content=$res['jd_union_open_goods_stuprice_query_responce']['queryResult'];
		$content = json_decode($content,true);
		return $content['data'];
	}
	//优惠券领取情况查询接口【申请】
	public function getcoupon($couponUrl){
		$param = array();
		$data['couponUrls'] = $couponUrl;
		$param['360buy_param_json'] = json_encode($data);
		$GoodsList = $this->sign_method('jd.union.open.goods.bigfield.query',$param);	
		$res = $this->curl_post('https://api.jd.com/routerjson', $GoodsList);
		$res = json_decode($res,true);
		if($res['code']!=0){
			exi('未获得《优惠券领取情况查询接口【申请】》API权限，请移步京东联盟申请','error');
		}
		$content=$res['jd_union_open_coupon_query_responce']['queryResult'];
		$content = json_decode($content,true);
		if($content['code']!=200){
			//exi('获取详情失败，错误代码：'.$content['code'],'error');
		}
		return $content['data'];
	}
	//订单行查询接口
	public function getorderrow($pageIndex,$pageSize=20,$type=1,$startTime='',$endTime='',$fields=array()){
		$endTime=$endTime?:time();
		$startTime=$startTime?:time()-60;
		$param = array();
		$data['orderReq'] = array(
			'pageIndex'=>$pageIndex,
			'pageSize'=>$pageSize,
			'type'=>$type,
			'startTime'=>$startTime,
			'endTime'=>$endTime,
		);
		$param['360buy_param_json'] = json_encode($data);
		$params = $this->sign_method('jd.union.open.order.row.query',$param);
		$res = $this->curl_post('https://api.jd.com/routerjson', $params);
		$res = json_decode($res,true);
		if($res['code']!=0){
			exi('未获得《订单行查询接口》API权限，请移步京东联盟申请','error');
		}
		$content=$res['jd_union_open_order_row_query_query_responce']['queryResult'];
		$content = json_decode($content,true);
		if($content['code']!=200){
			exi('获取订单失败，错误代码：'.$content['code'],'error');
		}
		return $content['data'];
	}
	//订单查询接口
	public function getorder($pageIndex=1,$pageSize=20,$type=1,$startTime='',$fields=array()){
		$startTime=$startTime?:time()-60;
		$param = array();
		$data['orderReq'] = array(
			'pageNo'=>$pageIndex,
			'pageSize'=>$pageSize,
			'type'=>$type,
			'time'=>$startTime,
		);
		$param['360buy_param_json'] = json_encode($data);
		$params = $this->sign_method('jd.union.open.order.query',$param);
		$res = $this->curl_post('https://api.jd.com/routerjson', $params);
		$res = json_decode($res,true);
		if($res['code']!=0){
			exi('未获得《订单查询接口》API权限，请移步京东联盟申请','error');
		}
		$content=$res['jd_union_open_order_query_responce']['queryResult'];
		$content = json_decode($content,true);
		if($content['code']!=200){
			exi('获取订单失败，错误代码：'.$content['code'],'error');
		}
		return $content['data'];
	}

	//工具商获取推广链接接口【申请】
	public function geturl($url){
		$basic=$this->plugin_setting('basic');
		$param = array();
		$data['promotionCodeReq'] = array(
			'materialId'=>$url,
			'unionId'=>$basic['unionid'],
			'chainType'=>3,
		);
		$param['360buy_param_json'] = json_encode($data);
		$params = $this->sign_method('jd.union.open.promotion.byunionid.get',$param);
		$res = $this->curl_post('https://api.jd.com/routerjson', $params);
		$res = json_decode($res,true);
		if($res['code']!=0){
			$this->getsiteurl($url);
		}
		$content=$res['jd_union_open_promotion_byunionid_get_responce']['getResult'];
		$content = json_decode($content,true);
		if($content['code']!=200){
			exi('获取推广链接失败，错误代码：'.$content['code'].$content['message'],'error');
		}
		return $content['data'];
	}
	//网站/APP获取推广链接接口
	public function getsiteurl($url,$mid=''){
		$mid=$mid?:$_SESSION['uid'];
		$basic=$this->plugin_setting('basic');
		$param = array();
		$data['promotionCodeReq'] = array(
			'materialId'=>$url,
			'unionId'=>$basic['unionid'],
			'siteId'=>$basic['siteid'],
			'ext1'=>$mid,
			'chainType'=>3,
		);
		$param['360buy_param_json'] = json_encode($data);
		$params = $this->sign_method('jd.union.open.promotion.common.get',$param);
		$res = $this->curl_post('https://api.jd.com/routerjson', $params);
		$res = json_decode($res,true);
		$content=$res['jd_union_open_promotion_common_get_responce']['getResult'];
		$content = json_decode($content,true);
		if($content['code']!=200){
			exi('获取推广链接失败，错误代码：'.$content['code'].$content['message'],'error');
		}
		return $content['data'];
	}
	public function curl_post($url, $curlPost)
    {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_REFERER, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
}
?>